﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;

/// <summary>
/// 图片处理
/// </summary>
public class ImageHandler
{
    #region 构造函数
    public ImageHandler(string _TempPath, string _SavePath)
    {
        tempPath = _TempPath;
        savePath = _SavePath;
    }

    public ImageHandler(string _TempPath, string _SavePath, int _Img_Width, int _Img_Height)
    {
        //改大小
        tempPath = _TempPath;
        savePath = _SavePath;
        Img_Width = _Img_Width;
        Img_Height = _Img_Height;
        IsChangeSize = true;
    }

    public ImageHandler(string _TempPath, string _SavePath, WaterMarkType _WMT, string _WM_Text_BmpPath)
    {
        //加水印
        tempPath = _TempPath;
        savePath = _SavePath;
        WaterMark = _WMT;
        WM_Text_BmpPath = _WM_Text_BmpPath;
    }
    #endregion

    #region 成员
    protected string tempPath = string.Empty;
    protected string savePath = string.Empty;

    protected bool isChangeSize = false;
    protected int img_Width = 200;
    protected int img_Height = 200;

    protected WaterMarkType watermark = WaterMarkType.None;
    protected string wm_Text_BmpPath = string.Empty;

    protected System.Drawing.Image SourceImage = null;
    protected System.Drawing.Image NewImage = null;
    Graphics g = null;
    #endregion

    #region 属性
    /// <summary>
    /// 是否改变大小
    /// </summary>
    public bool IsChangeSize
    {
        set { isChangeSize = value; }
    }
    /// <summary>
    /// 图片宽度
    /// </summary>
    public int Img_Width
    {
        set { img_Width = value; }
    }
    /// <summary>
    /// 图片高度
    /// </summary>
    public int Img_Height
    {
        set { img_Height = value; }
    }
    /// <summary>
    /// 水印类型
    /// </summary>
    public WaterMarkType WaterMark
    {
        set { watermark = value; }
    }
    /// <summary>
    /// 水印_( 文字 或 图片路径 )
    /// </summary>
    public string WM_Text_BmpPath
    {
        set { wm_Text_BmpPath = value; }
    }
    #endregion

    #region 枚举
    /// <summary>
    /// 水印类型
    /// </summary>
    public enum WaterMarkType
    {
        /// <summary>
        /// 不加
        /// </summary>
        None,
        /// <summary>
        /// 文字
        /// </summary>
        Text,
        /// <summary>
        /// 图片
        /// </summary>
        Bmp
    }
    #endregion

    #region 方法    
    /// <summary>
    /// 加工图片
    /// </summary>
    public virtual void Processing()
    {
        //加载图片
        SourceImage = Bitmap.FromFile(tempPath);        

        //改变大小
        if (isChangeSize) { ChangeSize(); }

        //加水印
        if (watermark != WaterMarkType.None) { AppendWatermark(); }

        //保存
        NewImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);        
                
        //释放资源
        g.Dispose();
        SourceImage.Dispose();
        NewImage.Dispose();
    }
    /// <summary>
    /// 改变图片大小
    /// </summary>
    protected void ChangeSize()
    {
        //如果大小有0，则没有必要了
        if (img_Width == 0 || img_Height == 0) { return; }
        
        //修改大小
        //SourceImage = SourceImage.GetThumbnailImage(img_Width, img_Height, null, IntPtr.Zero);
        NewImage = new Bitmap(img_Width, img_Height);
        g = Graphics.FromImage(NewImage);

        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(SourceImage, new Rectangle(0, 0, img_Width, img_Height));        
    }

    /// <summary>
    /// 追加水印
    /// </summary>
    protected void AppendWatermark()
    {
        System.Drawing.Image tempImg = (NewImage != null) ? NewImage : SourceImage;
        g = Graphics.FromImage(tempImg);
        switch (watermark)
        {
            case WaterMarkType.Text:
                //加文字水印
                g.DrawImage(tempImg, 0, 0, tempImg.Width, tempImg.Height);
                Font f = new Font("Verdana", 16);
                Brush b = new SolidBrush(Color.Blue);
                g.DrawString(wm_Text_BmpPath, f, b, 10, 10);

                break;

            case WaterMarkType.Bmp:
                //加图片水印
                System.Drawing.Image copyImage = Bitmap.FromFile(wm_Text_BmpPath);
                g.DrawImage(copyImage, new Rectangle(tempImg.Width - copyImage.Width, tempImg.Height - copyImage.Height,
                copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);

                break;
        }

        NewImage = tempImg;        
    }
    #endregion

}
